const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    piclist:[],
    Title:'',
    Photo:'',
    Messages:'',
    show:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  bindTextAreaTit:function (e) {
    this.setData({
      Title:e.detail.value
    })
  },
  bindTextAreaDesc:function (e) {
    this.setData({
      Messages:e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  uploadPic:function (e) {
    var that = this;
    wx.chooseImage({
      count:1,
      success (res) {
        const tempFilePaths = res.tempFilePaths;
        var api_input = {
           imgType: 22
        }
        var arr = app.creatSecretStr('Feijiu_UpyUpload_Img',JSON.stringify(api_input));
        wx.showLoading({
          title: "图片上传中"       
        }) 
        wx.uploadFile({
          url: app.data.api_url+'api/FeijiuApp/Feijiu_UpyUpload_Img',
          filePath: tempFilePaths[0],
          name: 'file',
          formData: arr,
          success (res){
            var data = JSON.parse(res.data);
            if (data.code == 200) {
              var list = that.data.piclist;
              list.push(data.data); 
              that.setData({
                piclist:list
              })             
            }
          },
          complete(res){
            wx.hideLoading();
          }
        })
      }
    }) 
  },
  delpic:function (e) {
    var idx = e.currentTarget.dataset.idx;
    var list = this.data.piclist;
    list.splice(idx, 1);
    this.setData({
      piclist:list
    })
  },
  tiwenbtn:function (e) {
    var that = this;
    var FjLoginLog = wx.getStorageSync('FjLoginLog');    
    if (this.data.Title.length < 5 ) {
      app.tishi('标题不能少于5个字!');return false;
    }   
    var api_input = {
      Title:this.data.Title,
      Photo:this.data.piclist.join('|'),
      Messages:this.data.Messages,
      UID:FjLoginLog.UID,
      Uname:FjLoginLog.UName,
      HeadPic:'',
    }
    app.getDaoPostN('api/FeijiuApp/Feijiu_ADD_Question',api_input,function (r) {      
      if (r.code == 200) {
        that.setData({
          show:true
        })
      }else{
        app.tishi('发布提问信息失败');
      }
    })
  },
  popguanbi:function () {
    this.setData({
      show:false
    })
    wx.navigateBack();
  },
  toindex:function () {
    wx.redirectTo({
      url: 'index'
    })
  }
})